/*
 * CalendarEv for jQuery -v1.0.0
 * 日历事件
 */
(function($) {
	"use strict";

	$.addOption = function(t, p) {
		p = $.extend({
			height: 'auto',
			width: 'auto',
			url: false, //请求路径
            date:false, //初始渲染的日期
			method: 'POST', //请求方法
			params: {}, //请求参数
			lang: 'zh', //语言
			type: 'daily', //类型 daily 日 week 周 month 月
			addEvent: false, //空白点击事件  可做优化 点击后渲染单个日期
			evContent: [{ //内容事件
				name: 'view',
                title:'查看',
				icon: '<svg t="1597911977542" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4792" width="25" height="25"><path d="M455.68 770.56c-169.472 0-307.2-137.728-307.2-307.2s137.728-306.688 307.2-306.688 307.2 137.728 307.2 306.688c-0.512 169.472-138.24 307.2-307.2 307.2z m0-547.328c-132.608 0-240.64 108.032-240.64 240.128 0 132.608 108.032 240.64 240.64 240.64s240.64-108.032 240.64-240.64c-0.512-132.608-108.544-240.128-240.64-240.128zM855.04 888.32c-8.704 0-16.896-3.072-23.552-9.728l-113.664-113.664c-12.8-12.8-12.8-34.304 0-47.104 12.8-12.8 34.304-12.8 47.104 0l113.664 113.664c12.8 12.8 12.8 34.304 0 47.104-6.656 6.656-14.848 9.728-23.552 9.728z" p-id="4793" fill="#00abab"></path></svg>',
				event: function() {
					alert('查看');
				},
			}, {
				name: 'rebuild',
                title:'重新生成',
				icon: '<svg t="1597912085291" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8789" width="25" height="25"><path d="M842 62H182c-66 0-120 54-120 120v660c0 66 54 120 120 120h660c66 0 120-54 120-120V182c0-66-54-120-120-120z m30 750c0 33-27 60-60 60H212c-33 0-60-27-60-60V212c0-33 27-60 60-60h600c33 0 60 27 60 60v600z" fill="#ffaa7f" p-id="8790"></path><path d="M737 467H557V287c0-24.8-20.2-45-45-45s-45 20.2-45 45v180H287c-24.8 0-45 20.2-45 45s20.2 45 45 45h180v180c0 24.8 20.2 45 45 45s45-20.2 45-45V557h180c24.8 0 45-20.2 45-45s-20.2-45-45-45z" fill="#ffaa7f" p-id="8791"></path></svg>',
				event: function() {
					alert('重建');
				},
			}, {
				name: 'download',
                title:'下载',
				icon: '<svg t="1597912139156" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9937" width="25" height="25"><path d="M919.724075 715.073068c-1.140986-27.353998-19.024286-42.460044-42.618657-41.899272-23.378453 0.554632-38.799678 15.928784-40.99467 43.88858-1.610684 20.539802-0.231267 41.299615-0.676405 61.951981-1.115404 51.828412-12.339026 63.804165-64.151066 64.826448-49.90869 0.985444-99.853196 0.323365-149.781328 0.327458-115.351168 0.00921-230.704382 0.094144-346.05555-0.065492-79.440283-0.109494-86.325088-6.972808-86.704734-84.203774-0.068562-13.770632 0.62524-27.626197-0.639566-41.293475-2.692319-29.087479-16.327874-44.221154-39.414685-45.526893-24.920576-1.411139-43.084261 15.533788-44.989657 45.49517-1.74269 27.442002-0.763386 55.07127-0.582261 82.615603 0.590448 89.169879 40.095183 129.182174 129.293715 129.691781 92.966345 0.532119 185.938829 0.118704 278.909268 0.140193 27.54638 0.022513 55.094806 0.031722 82.640162 0.021489 70.589708-0.024559 141.195789 0.956791 211.758891-0.39295 62.212924-1.189082 107.706047-37.176714 112.958678-91.756797C922.616962 797.940404 921.441183 756.292865 919.724075 715.073068z" p-id="9938" fill="#55aaff"></path><path d="M197.8583 440.865893c88.756463 88.806605 177.525206 177.601954 266.292925 266.398326 3.647063 3.64911 7.361665 7.230682 10.984169 10.902304 14.284331 14.481829 30.947849 22.644743 51.134611 14.996552 6.584975-2.494821 11.479449-6.255471 15.664771-10.522658 2.762927-1.912559 5.470595-4.0175 7.9296-6.407943 45.635363-44.375673 240.102427-239.467977 285.027615-284.574291 4.825912-4.845355 9.463536-10.05296 13.269211-15.71082 12.214183-18.164708 10.541077-35.95898-3.945869-51.853995-14.818497-16.257266-33.480532-16.939811-51.874461-7.894807-8.930393 4.391007-16.285918 12.4608-23.556509 19.673062-30.239721 29.991058-139.611711 139.26481-212.845643 212.610282 0.037862-114.560152 0.050142-229.119281 0.027629-343.680457-0.00614-34.398438 1.090844-68.859297-0.615007-103.175871-1.587148-31.878035-17.058514-48.140417-41.968857-48.957015-25.551956-0.838088-41.055045 14.728446-44.538379 46.158273-1.320065 11.908214-0.623193 24.053835-0.629333 36.09201-0.040932 75.676563-0.021489 151.355173-0.021489 227.033783-0.001023 56.590879 0 113.181759 0 169.773661-3.928472 1.738597-7.855922 3.476171-11.784394 5.214768-22.74912-22.663162-45.525869-45.303812-68.245314-67.99665-47.452755-47.405683-94.28948-95.445816-142.575206-141.985782-21.800515-21.013593-47.071062-21.358447-64.429405-4.840238-18.709107 17.803481-18.050098 39.000246 2.325975 63.951521C187.818643 431.374728 192.996572 436.000072 197.8583 440.865893z" p-id="9939" fill="#55aaff"></path></svg>',
				event: function() {
					alert('下载');
				},
			}], //事件
			fillText: false,//文本
			onSuccess:false,//后台请求成功回调
			onError:false,//后台请求失败回调
		}, p);

		$(t).empty();

		$(t).show();

		var f = {
			daily: {
				/**
				 * 更新日历
				 * @param {Object} year
				 * @param {Object} month
				 * @param {Object} day
				 */
				render: function(year, month, day) {
					if (typeof year === 'undefined' && typeof month === 'undefined' && typeof day === 'undefined') {
						var nowDate = new Date();
						year = nowDate.getFullYear();
						month = nowDate.getMonth();
						day = nowDate.getDate();
					}
					// 更新一下顶部的年月显示
					$('#nowDate').html(g.dateFormat2MonthString(new Date(year, month, 1))); //由于2月没有30号，此处写死1

					// 生成日历数据，上个月剩下的的 x 天 + 当月的 28（平年的2月）或者29（闰年的2月）或者30或者31天 + 下个月的 y 天 = 42
					var res = [];
					var currentMonth = g.getMonthCount(year, month);
					var preMonth = g.getPreMonthCount(year, month);
					var nextMonth = g.getNextMonthCount(year, month);
					var whereMonday = g.getWeekday(year, month);
					if (whereMonday === 0) {
						whereMonday = 7;
					}
					// 这里当 whereMonday 为 0 的时候会截取上月的所有数据
					var preArr = preMonth.slice(-1 * whereMonday);
					var nextArr = nextMonth.slice(0, 42 - currentMonth.length - whereMonday);
					res = [].concat(preArr, currentMonth, nextArr);
					var hadDom = document.getElementsByClassName('date-item');

					g.init();
					
					if (hadDom && hadDom.length) {
						var domArr = document.getElementsByClassName('date-item');
						for (var i = 0, len = domArr.length; i < len; i++) {
							var data = res.shift();
							if (i < preArr.length || i >= (len - nextArr.length)) {
								$(domArr[i]).html('<div class="content">' + data + '</div><div class="date-content"></div>');
								$(domArr[i]).addClass('disabled');
							} else {
								$(domArr[i]).html('<div class="content">' + data + '</div><div class="date-content addEv" data-date=' +
									year +
									"-" + g.complementzero(month+1) + "-" + g.complementzero(data) + '><span>' + g.addIcon + '</span></div>');
								$(domArr[i]).removeClass('disabled');
							}
						}

						$(document).off('click', '.date-line .addEv span').on('click', '.date-line .addEv span', function() {
							if (p.addEvent.event && typeof p.addEvent.event == 'function') {
								p.addEvent.event.call(this, $(this).parent().attr('data-date'));
							}
						});
					} else {
						var str = '';
						for (var i = 0; i < 6; i++) {
							str += '<div class="date-line">';
							for (var j = 0; j < 7; j++) {
								var data = res.shift();
								if (res.length >= (currentMonth.length + nextArr.length) || res.length < nextArr.length) {
									str +=
										'<div class="date-item disabled"><div class="content">'+data+'</div><div class="date-content"></div></div>';
								} else {
									str += '<div class="date-item"><div class="content">' + data +
										'</div><div class="date-content addEv" data-date=' + year + "-" + g.complementzero(month+1) + "-" + g.complementzero(data) + '><span>' + g.addIcon +
										'</span></div></div>';
								}
								if (j === 6) {
									str += '</div>';
								}
							}
						}
						$('#dateWrap').html(str);

						$(document).on('click', '.date-line .addEv span', function() {
							if (p.addEvent.event && typeof p.addEvent.event == 'function') {
								p.addEvent.event.call(this, $(this).parent().attr('data-date'));
							}
						});
					}
					//计算宽度
					this.calculateWidth();
					//获取后台结果
					g.populate();
				},
				/**
				 * 添加头部
				 */
				addHead: function() {
					var lang = g.lang();
					var $dateLine = $('<div id="weekLine" class="week-line"></div>');
					var $dateWrap = $('<div id="dateWrap" class="date-wrap"></div>');
					var date = g.dateFormat2MonthString(new Date());
					var $year = $(
						'<div id="year-line"><div id="preMonth" class="month-prev">' + lang.lastMonth +
						'</div><div id="nowDate" class="date-now">' + date + '</div><div id="nextMonth" class="month-next">' + lang.nextMonth +
						'</div></div>'
					);
					$(t).append($year);
					$(t).append($dateLine);
					$(t).append($dateWrap);
				},
				/**
				 * 添加主体
				 */
				addBody: function(date) {
					var lang = g.lang(),
						that = this,
						weekArr = [];

					lang.weekStr.split(',').forEach(function(item, index) {
						weekArr[index] = lang.week + item;
					});

					// 插入星期 dom
					var weekDomStr = '',
						oFragWeek = document.createDocumentFragment();

					weekArr.forEach(function(item) {
						var oDiv = document.createElement('div');
						var oText = document.createTextNode(item);
						oDiv.appendChild(oText);
						oDiv.classList.add('week-item');
						oFragWeek.appendChild(oDiv);
					});

					var weekWrap = document.getElementById('weekLine');
					weekWrap.appendChild(oFragWeek);
					//更新日历
                    if (date){
                        var dateArray = date.split('-');
                        this.render(parseInt(dateArray[0]),parseInt(dateArray[1])-1,parseInt(dateArray[2]));
                    }else{
                        this.render();
                    }

					// 添加上一月，下一月事件
					var oPreButton = document.getElementById('preMonth');
					var oNextButton = document.getElementById('nextMonth');
					oPreButton.addEventListener('click', function() {
						var dateStr = $('#nowDate').text();
						var date = g.dateStrDate2MonthDate(dateStr);
						var currentYear = date.getFullYear();
						var currentMonth = date.getMonth();
						var currentDate = new Date().getDate();
						if (currentMonth === 0) {
							that.render(currentYear - 1, 11, currentDate);
						} else {
							that.render(currentYear, currentMonth - 1, currentDate);
						}
					});
					oNextButton.addEventListener('click', function() {
						var dateStr = $('#nowDate').text();
						var date = g.dateStrDate2MonthDate(dateStr);
						var currentYear = date.getFullYear();
						var currentMonth = date.getMonth();
						var currentDate = new Date().getDate();
						if (currentMonth === 11) {
							that.render(currentYear + 1, 0, currentDate);
						} else {
							that.render(currentYear, currentMonth + 1, currentDate);
						}
					});
				},
				/**
				 * 计算宽度
				 */
				calculateWidth: function(){
					var itemLength = $('.date-line:first .date-item').length;
					if(p.width === 'auto'){
						$('.week-line .week-item,.date-item').width((100/itemLength) + '%');
						$('.week-line .week-item,.date-item').css('min-width',140);
					}else{
						var width = p.width/itemLength;
						width = width < 140 ? 140 : width;
						$('.week-line .week-item,.date-item').width(width);
						$('.week-line .week-item,.date-item').css('min-width',width);
					}
					if(p.height === 'auto'){
						p.height = $(window).height();
					}
					var bodyHeight = p.height - $('#year-line').height() - $('#weekLine').height() - 50;
					$('.date-item').height(bodyHeight / 6);
				}
			},
			week: {
				/**
				 * 更新日历
				 * @param {Object} year
				 */
				render: function(year) {
					var lang = g.lang();
					if (typeof year === 'undefined') {
						year = new Date().getFullYear();
					}
					// 更新一下顶部的年显示
					$('#nowDate').html(year + lang.year);
					
					// 生成日历数据
					var weeks = g.getWeekByYear(year);
					var hadDom = document.getElementsByClassName('date-item');
					
					g.init();
					
					var str = '';
					var week = 1;
					for (var i = 1; i <= 6; i++) {
						str += '<div class="date-line">';
						for (var k = 1; k >= 0; k--) {
							str += '<div class="date-head">'+(i*2-k)+lang.month+'</div>';// 1 2 
							for (var j = 0; j < 5; j++) {
								if(weeks[i*2-k][j]){
									str += '<div class="date-item"><div class="content"><span>'+(week)+lang.weeks+'</span><span class="weekRange">'+weeks[i*2-k][j]+'</span></div><div class="date-content addEv" data-date='+year+"-"+g.complementzero(week)+'><span>'+g.addIcon+'</span></div></div>';
									week++;
								}else{
									str += '<div class="date-item"><div class="content"></div></div>';		
								}
							}
						}
						str += '</div>';
					}
					
					$('#dateWrap').html(str);
					
					if (hadDom && hadDom.length) {
						$(document).off('click', '.date-line .addEv span').on('click', '.date-line .addEv span', function() {
							if (p.addEvent.event && typeof p.addEvent.event == 'function') {
								p.addEvent.event.call(this, $(this).parent().attr('data-date'));
							}
						});
					} else {
						$(document).on('click', '.date-line .addEv span', function() {
							if (p.addEvent.event && typeof p.addEvent.event == 'function') {
								p.addEvent.event.call(this, $(this).parent().attr('data-date'));
							}
						});
					}
					//计算宽度
					this.calculateWidth();
					//获取后台结果
					g.populate();
				},
				/**
				 * 添加头部
				 */
				addHead: function() {
					var lang = g.lang();
					var $dateLine = $('<div id="weekLine" class="week-line"></div>');
					var $dateWrap = $('<div id="dateWrap" class="date-wrap"></div>');
					var date = new Date().getFullYear() + lang.year;
					var $year = $(
						'<div id="year-line"><div id="preYear" class="year-prev">' + lang.lastYear +
						'</div><div id="nowDate" class="date-now">' + date + '</div><div id="nextYear" class="year-next">' + lang.nextYear +
						'</div></div>'
					);
					$(t).append($year);
					$(t).append($dateLine);
					$(t).append($dateWrap);
				},
				/**
				 * 添加主体
				 */
				addBody: function(date) {
					var lang = g.lang(),
						that = this;
					
					//更新日历
                    if (date){
                        this.render(date);
                    }else{
                        this.render();
                    }

					// 添加上一年，下一年事件
					var oPreButton = document.getElementById('preYear');
					var oNextButton = document.getElementById('nextYear');
					oPreButton.addEventListener('click', function() {
						var date = parseInt($('#nowDate').text());
						that.render(date - 1);
					});
					oNextButton.addEventListener('click', function() {
						var date = parseInt($('#nowDate').text());
						that.render(date + 1);
					});
				},
				/**
				 * 计算宽度
				 */
				calculateWidth: function(){
					var itemLength = $('.date-line:first .date-item').length;
					var headLength = 2;
					if(p.width === 'auto'){
						$('.date-item').width(($(window).width() - headLength*68)/itemLength);
						$('.date-item').css('min-width',140);
					}else{
						var width = (p.width - headLength*68)/itemLength;
						width = width < 140 ? 140 : width;
						$('.date-item').width(width);
						$('.date-item').css('min-width',width);
					}
					if(p.height === 'auto'){
						p.height = $(window).height();
					}
					var bodyHeight = p.height - $('#year-line').height() - $('#weekLine').height() - 50;
					$('.date-head,.date-item').height(bodyHeight / 6);
					$('.date-head').css('line-height',(bodyHeight / 6) + 'px');
				}
			},
			month: {
                /**
				 * 更新日历
				 * @param {Object} year
				 */
				render: function(year) {
					var lang = g.lang();
					if (typeof year === 'undefined') {
						year = new Date().getFullYear();
					}
					// 更新一下顶部的年显示
					$('#nowDate').html(year + lang.year);
					
					var hadDom = document.getElementsByClassName('date-item');
					
					g.init();
					
					var str = '';

					for (var i = 1; i <= 6; i++) {
						str += '<div class="date-line">';
						for (var k = 1; k >= 0; k--) {
							str += '<div class="date-head">'+(i*2-k)+lang.month+'</div>';// 1 2 
							str += '<div class="date-item"><div class="content"></div><div class="date-content addEv" data-date='+year+"-"+g.complementzero(i*2-k)+'><span>'+g.addIcon+'</span></div></div>';
						}
						str += '</div>';
					}
					
					$('#dateWrap').html(str);
					
					if (hadDom && hadDom.length) {
						$(document).off('click', '.date-line .addEv span').on('click', '.date-line .addEv span', function() {
							if (p.addEvent.event && typeof p.addEvent.event == 'function') {
								p.addEvent.event.call(this, $(this).parent().attr('data-date'));
							}
						});
					} else {
						$(document).on('click', '.date-line .addEv span', function() {
							if (p.addEvent.event && typeof p.addEvent.event == 'function') {
								p.addEvent.event.call(this, $(this).parent().attr('data-date'));
							}
						});
					}
					//计算宽度
					this.calculateWidth();
					//获取后台结果
					g.populate();
				},
				/**
				 * 添加头部
				 */
				addHead: function() {
					f.week.addHead();
				},
				/**
				 * 添加主体
				 */
				addBody: function(date) {
					var lang = g.lang(),
						that = this;
					
					//更新日历
                    if (date){
                        this.render(date);
                    }else{
                        this.render();
                    }

					// 添加上一年，下一年事件
					var oPreButton = document.getElementById('preYear');
					var oNextButton = document.getElementById('nextYear');
					oPreButton.addEventListener('click', function() {
						var date = parseInt($('#nowDate').text());
						that.render(date - 1);
					});
					oNextButton.addEventListener('click', function() {
						var date = parseInt($('#nowDate').text());
						that.render(date + 1);
					});
				},
				/**
				 * 计算宽度
				 */
				calculateWidth: function(){
					var itemLength = $('.date-line:first .date-item').length;
					var headLength = 2;
					if(p.width === 'auto'){
						$('.date-item').width(($(window).width() - headLength*68)/itemLength);
						$('.date-item').css('min-width',140);
					}else{
						var width = (p.width - headLength*68)/itemLength;
						width = width < 140 ? 140 : width;
						$('.date-item').width(width);
						$('.date-item').css('min-width',width);
					}
					
					if(p.height === 'auto'){
						p.height = $(window).height();
					}
					var bodyHeight = p.height - $('#year-line').height() - $('#weekLine').height() - 50;
					$('.date-head,.date-item').height(bodyHeight / 6);
					$('.date-head').css('line-height',(bodyHeight / 6) + 'px');
				}
			}
		}



		var g = {
			addIcon: '<svg t="1597745267853" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1165" width="16" height="16"><path d="M512 0c282.752 0 512 229.248 512 512s-229.248 512-512 512S0 794.752 0 512 229.248 0 512 0z m0 213.333333a42.666667 42.666667 0 0 0-42.666667 42.666667v213.333333H256l-4.992 0.298667A42.666667 42.666667 0 0 0 256 554.666667h213.333333v213.333333l0.298667 4.992A42.666667 42.666667 0 0 0 554.666667 768v-213.333333h213.333333l4.992-0.298667A42.666667 42.666667 0 0 0 768 469.333333h-213.333333V256l-0.298667-4.992A42.666667 42.666667 0 0 0 512 213.333333z" fill="#0bb4f2" p-id="1166"></path></svg>',
			lang: function() {
				var text = {
					zh: {
						week: '星期',
						weekStr: '日,一,二,三,四,五,六',
						lastMonth: '上一月',
						nextMonth: '下一月',
						lastYear: '上一年',
						nextYear: '下一年',
						year: '年',
						month: '月',
						weeks: '周',
					},
					en: {
						week: '',
						weekStr: 'Sun,Mon,Tues,Wed,Thur,Fri,Sat',
						lastMonth: 'Last Month',
						nextMonth: 'Next Month',
						lastYear: 'Last Year',
						nextYear: 'Next Year',
						year: '',
						month: '',
						weeks: 'Week',
					},
					tw: {
						week: '星期',
						weekStr: '日,一,二,三,四,五,六',
						lastMonth: '上一月',
						nextMonth: '下一月',
						lastYear: '上一年',
						nextYear: '下一年',
						year: '年',
						month: '月',
						weeks: '週',
					}
				}
				return text[p.lang] || text['zh'];
			},
            icon:{
			    'view':'<svg t="1597911977542" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4792" width="25" height="25"><path d="M455.68 770.56c-169.472 0-307.2-137.728-307.2-307.2s137.728-306.688 307.2-306.688 307.2 137.728 307.2 306.688c-0.512 169.472-138.24 307.2-307.2 307.2z m0-547.328c-132.608 0-240.64 108.032-240.64 240.128 0 132.608 108.032 240.64 240.64 240.64s240.64-108.032 240.64-240.64c-0.512-132.608-108.544-240.128-240.64-240.128zM855.04 888.32c-8.704 0-16.896-3.072-23.552-9.728l-113.664-113.664c-12.8-12.8-12.8-34.304 0-47.104 12.8-12.8 34.304-12.8 47.104 0l113.664 113.664c12.8 12.8 12.8 34.304 0 47.104-6.656 6.656-14.848 9.728-23.552 9.728z" p-id="4793" fill="#00abab"></path></svg>',
                'rebuild':'<svg t="1597912085291" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8789" width="25" height="25"><path d="M842 62H182c-66 0-120 54-120 120v660c0 66 54 120 120 120h660c66 0 120-54 120-120V182c0-66-54-120-120-120z m30 750c0 33-27 60-60 60H212c-33 0-60-27-60-60V212c0-33 27-60 60-60h600c33 0 60 27 60 60v600z" fill="#ffaa7f" p-id="8790"></path><path d="M737 467H557V287c0-24.8-20.2-45-45-45s-45 20.2-45 45v180H287c-24.8 0-45 20.2-45 45s20.2 45 45 45h180v180c0 24.8 20.2 45 45 45s45-20.2 45-45V557h180c24.8 0 45-20.2 45-45s-20.2-45-45-45z" fill="#ffaa7f" p-id="8791"></path></svg>',
                'download':'<svg t="1597912139156" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9937" width="25" height="25"><path d="M919.724075 715.073068c-1.140986-27.353998-19.024286-42.460044-42.618657-41.899272-23.378453 0.554632-38.799678 15.928784-40.99467 43.88858-1.610684 20.539802-0.231267 41.299615-0.676405 61.951981-1.115404 51.828412-12.339026 63.804165-64.151066 64.826448-49.90869 0.985444-99.853196 0.323365-149.781328 0.327458-115.351168 0.00921-230.704382 0.094144-346.05555-0.065492-79.440283-0.109494-86.325088-6.972808-86.704734-84.203774-0.068562-13.770632 0.62524-27.626197-0.639566-41.293475-2.692319-29.087479-16.327874-44.221154-39.414685-45.526893-24.920576-1.411139-43.084261 15.533788-44.989657 45.49517-1.74269 27.442002-0.763386 55.07127-0.582261 82.615603 0.590448 89.169879 40.095183 129.182174 129.293715 129.691781 92.966345 0.532119 185.938829 0.118704 278.909268 0.140193 27.54638 0.022513 55.094806 0.031722 82.640162 0.021489 70.589708-0.024559 141.195789 0.956791 211.758891-0.39295 62.212924-1.189082 107.706047-37.176714 112.958678-91.756797C922.616962 797.940404 921.441183 756.292865 919.724075 715.073068z" p-id="9938" fill="#55aaff"></path><path d="M197.8583 440.865893c88.756463 88.806605 177.525206 177.601954 266.292925 266.398326 3.647063 3.64911 7.361665 7.230682 10.984169 10.902304 14.284331 14.481829 30.947849 22.644743 51.134611 14.996552 6.584975-2.494821 11.479449-6.255471 15.664771-10.522658 2.762927-1.912559 5.470595-4.0175 7.9296-6.407943 45.635363-44.375673 240.102427-239.467977 285.027615-284.574291 4.825912-4.845355 9.463536-10.05296 13.269211-15.71082 12.214183-18.164708 10.541077-35.95898-3.945869-51.853995-14.818497-16.257266-33.480532-16.939811-51.874461-7.894807-8.930393 4.391007-16.285918 12.4608-23.556509 19.673062-30.239721 29.991058-139.611711 139.26481-212.845643 212.610282 0.037862-114.560152 0.050142-229.119281 0.027629-343.680457-0.00614-34.398438 1.090844-68.859297-0.615007-103.175871-1.587148-31.878035-17.058514-48.140417-41.968857-48.957015-25.551956-0.838088-41.055045 14.728446-44.538379 46.158273-1.320065 11.908214-0.623193 24.053835-0.629333 36.09201-0.040932 75.676563-0.021489 151.355173-0.021489 227.033783-0.001023 56.590879 0 113.181759 0 169.773661-3.928472 1.738597-7.855922 3.476171-11.784394 5.214768-22.74912-22.663162-45.525869-45.303812-68.245314-67.99665-47.452755-47.405683-94.28948-95.445816-142.575206-141.985782-21.800515-21.013593-47.071062-21.358447-64.429405-4.840238-18.709107 17.803481-18.050098 39.000246 2.325975 63.951521C187.818643 431.374728 192.996572 436.000072 197.8583 440.865893z" p-id="9939" fill="#55aaff"></path></svg>'
            },
			/**
			 * 是否闰年
			 */
			isLeapYear: function(year) {
				return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
			},
			/**
			 * 转换成 2012-05 的格式		短时间格式
			 */
			dateFormat2MonthString: function(date) {
				return date.getFullYear() + "-" + this.dateFormatValue(date.getMonth() + 1);
			},
			dateFormatValue: function(value) {
				if (value <= 9) {
					return "0" + value;
				} else {
					return value;
				}
			},
			/**
			 * 字符(长时间2012-04）转换成时间
			 */
			dateStrDate2MonthDate: function(str) {
				var reg = /^(\d{1,4})(-|\/)(\d{1,2})$/;
				var r = str.match(reg);
				if (r == null) {
					return false;
				}

				var d = new Date(r[1], r[3] - 1);
				return d;
			},
			/**
			 * 获得每个月的日期有多少，注意 month - [0-11]
			 */
			getMonthCount: function(year, month) {
				var arr = [
					31, null, 31, 30,
					31, 30, 31, 31,
					30, 31, 30, 31
				];
				var count = arr[month] || (this.isLeapYear(year) ? 29 : 28);
				var result = [];
				for (var i = 0; i < count; i++) {
					result[i] = i + 1;
				}
				return result;
			},
			/**
			 * 获得某年某月的 1号 是星期几，这里要注意的是 JS 的 API-getDay() 是从 [日-六](0-6)，返回 number
			 */
			getWeekday: function(year, month) {
				var date = new Date(year, month, 1);
				return date.getDay();
			},
			/**
			 * 获得上个月的天数
			 */
			getPreMonthCount: function(year, month) {
				if (month === 0) {
					return this.getMonthCount(year - 1, 11);
				} else {
					return this.getMonthCount(year, month - 1);
				}
			},
			/**
			 * 获得下个月的天数
			 */
			getNextMonthCount: function(year, month) {
				if (month === 11) {
					return this.getMonthCount(year + 1, 0);
				} else {
					return this.getMonthCount(year, month + 1);
				}
			},
			/**
			 * 获取一年有多少周，从每年的1-1开始
			 */
			getWeekByYear: function(year) {
				var weeks = [];
				var d = new Date(year, 0, 1);
				//每年的1月1号开始到星期日为第一周
				var to = new Date(year + 1, 0, 1);
				var i = 0;
				for (var from = d; from < to;) {
					var month = (from.getMonth() + 1);
					var date = (from.getMonth() + 1) + '/' + from.getDate();
					//document.write(year + "年第" + i + "周 " + (from.getMonth() + 1) + "月" + from.getDate() + "日 - ");
					//from.setDate(from.getDate() + 6);
					if (i == 0) {
						var days = 7 - from.getDay();
						from.setDate(from.getDate() + days);
					} else {
						from.setDate(from.getDate() + 6);
					}
					if (from < to) {
						date += ' ~ ' + (from.getMonth() + 1) + '/' + from.getDate(); 
						// document.write((from.getMonth() + 1) + "月" + from.getDate() + "日<br / >");
						from.setDate(from.getDate() + 1);
					} else {
						to.setDate(to.getDate() - 1);
						date += ' ~ ' + (to.getMonth() + 1) + '/' + to.getDate();
						//document.write((to.getMonth() + 1) + "月" + to.getDate() + "日<br / >");
					}
					weeks[month] = weeks[month] || [];
					weeks[month].push(date);
					i++;
				}
				return weeks;
			},
			/**
			 * 根据年和周日期范围
			 */
			getDateRange: function(_year, _week) {
				var beginDate;
				var endDate;
				if (_year == null || _year == '' || _week == null || _week == '') {
					return "";
				}
				beginDate = this.getXDate(_year, _week - 1, 1);
				endDate = this.getXDate(_year, _week, 0);
				return this.getNowFormatDate(beginDate) + "|" + this.getNowFormatDate(endDate);
			},
			/**
			 * 某年某周获取一周的日期
			 * @param {Object} year
			 * @param {Object} weeks
			 * @param {Object} weekDay
			 */
			getXDate: function(year, weeks, weekDay) {
				var date = new Date(year, "0", "1");
				var newDate = new Date(year, "0", "1");
				if (weeks == 0 && weekDay == 1) {
					return date;
				}
				if (weeks == 1 && weekDay == 0) {
					return this.getNextDate(date, 0, date);
				}
				var time = date.getTime();
				time += (weeks - 1) * 7 * 24 * 3600000;
				// 为日期对象 date 重新设置成时间 time
				newDate.setTime(time);
				return this.getNextDate(newDate, weekDay, date);
			},
			/**
			 * 取得 某日期(nowDate) 所在周的星期几(weekDay)的日期
			 * @param {Object} nowDate
			 * @param {Object} weekDay
			 * @param {Object} date
			 */
			getNextDate: function(nowDate, weekDay, date) {
				// 0是星期日,1是星期一,...
				weekDay %= 7;
				var day = nowDate.getDay();
				var time = nowDate.getTime();
				var sub = weekDay - day;
				if (sub <= 0) {
					sub += 7;
				}
				time += sub * 24 * 3600000;
				nowDate.setTime(time);
				var nextYear = new Date(date.getFullYear() + 1, "0", "1");
				if (nowDate >= nextYear) {
					time = nextYear.getTime() - 24 * 3600000;
					nowDate.setTime(time);
				}
				return nowDate;
			},
			getNowFormatDate: function(theDate) {
				var day = theDate;
				var Year = 0;
				var Month = 0;
				var Day = 0;
				var CurrentDate = "";
				// 初始化时间
				Year = day.getFullYear(); // ie火狐下都可以
				Month = day.getMonth() + 1;
				Day = day.getDate();
				CurrentDate += Year + "-";
				if (Month >= 10) {
					CurrentDate += Month + "-";
				} else {
					CurrentDate += "0" + Month + "-";
				}
				if (Day >= 10) {
					CurrentDate += Day;
				} else {
					CurrentDate += "0" + Day;
				}
				return CurrentDate;
			},
            complementzero:function(date){
			    return date >= 10 ? date : '0' + date;
            },
			init: function(){
				if (p.addEvent && p.addEvent.icon) {
					g.addIcon = p.addEvent.icon;
				}
				if (!p.addEvent || !p.addEvent.event) {
					p.addEvent.event = null;
				}
			},
			populate: function() {
			    if (p.params){
                    var year = $('#year-line #nowDate').text();
			        if (p.type === 'daily'){
                        p.params.beginTime = year + '-01';
                        p.params.endTime = year + '-31';
                    }else if (p.type === 'week'){
			            year = year.substring(0,year.length-1);
                        p.params.beginTime = year + '-01';
                        p.params.endTime = year + '-54';
                    }else if (p.type === 'month'){
                        year = year.substring(0,year.length-1);
                        p.params.beginTime = year + '-01';
                        p.params.endTime = year + '-12';
                    }
                }
				var that = this;
				$.ajax({
					type: p.method,
					url: p.url,
					data: p.params,
					dataType: 'json',
					success: function(data) {
						if (data.result == 0) {
							that.addData(data.data);
							if(p.onSuccess && typeof p.onSuccess == 'function'){
								p.onSuccess.call(this,data.data);
							}
						} else {
							showErrorMessage(data.result);
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						try {
							if (p.onError) p.onError(XMLHttpRequest, textStatus, errorThrown);
						} catch (e) {}
					}
				});
			},
			/**
			 * 添加数据
			 * @param {Object} data
			 */
			addData: function(data) {
				if (!data) {
					return;
				}
				$('.date-line .date-item:not(.disabled)').each(function(i, ele) {
					for (var i = 0, len = data.length; i < len; i++) {
						if ($(ele).find('.date-content').attr('data-date') == data[i].date) {
							var text = '';
							if (p.fillText && typeof p.fillText == 'function') {
								text = p.fillText(data[i]);
							}
							var contentHtml = '<div>' + text + '</div>';
			
							if (p.evContent && p.evContent.length > 0) {
								var html = '<div class="evContent" style="margin-top:10px">';
								p.evContent.forEach(function(item, index) {
									html += '<span class="' + item.name + '" title="'+item.title+'" style="margin:0px 10px 0px 10px">';
									if (item.icon) {
										html += item.icon;
									}else{
									    if (g.icon[item.name]){
                                            html += g.icon[item.name];
                                        }
                                    }
									html += '</span>';
								})
								html += '</div>';
								contentHtml += html;
							}
							$(ele).find('.date-content').removeClass('addEv').html(contentHtml);
						}
					}
				});
			
				//绑定事件
				if (p.evContent && p.evContent.length > 0) {
					p.evContent.forEach(function(item, index) {
						$(document).off('click', '.date-item .evContent .' + item.name).on('click', '.date-item .evContent .' +
							item.name,
							function() {
								if (item.event && typeof item.event == 'function') {
									item.event.call(this, $(this).parents('.date-content').attr('data-date'));
								}
							});
					});
				};
			
			},
		}

		//添加头部
		f[p.type].addHead();
		//添加主体
		f[p.type].addBody(p.date);

	};



	var docloaded = false;

	$(document).ready(function() {
		docloaded = true;
	});

	$.fn.evDate = function(p) {
		return this.each(function() {
			if (!docloaded) {
				$(this).hide();
				var t = this;
				$(document).ready(function() {
					$.addOption(t, p);
				});
			} else {
				$.addOption(this, p);
			}
		});
	};
















})(jQuery);
